﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using System.Data;
using DAL.Connect;
namespace DAL.Classes
{
    public class BinhLuan
    {
        #region khai bao cac thuoc tinh anh xa
        public int MaBinhLuan { get; set; }
        public int MaBaiDang { get; set; }
        public string MaTaiKhoan { get; set; }
        public string NoiDung { get; set; }
        public DateTime NgayTao { get; set; }
        public string TenDangNhap { get; set; }
        public BinhLuan() { }
        #endregion

        #region Cac phuong thuc them xoa sua
        public static bool Them(BinhLuan binhLuan)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@MaBinhLuan", "BinhLuan_Them",
                    binhLuan.MaBinhLuan, binhLuan.MaBaiDang, binhLuan.MaTaiKhoan, binhLuan.NoiDung,
                    binhLuan.NgayTao);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool Sua(BinhLuan binhLuan)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("BinhLuan_Sua",
                    binhLuan.MaBinhLuan, binhLuan.MaTaiKhoan, binhLuan.NoiDung);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool Xoa(string maBinhLuan)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("BinhLuan_Xoa", Convert.ToInt32(maBinhLuan));
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Cac phuong thuc lay du lieu
        public static List<BinhLuan> LayBinhLuanTheoBaiDang(string maBaiDang, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = 8;
                reader = DataProvider.Instance.ExecuteReader("BinhLuan_LayTheoMaBaiDang_PT", Convert.ToInt32(maBaiDang),
                                                            GlobalConfiguration.DescriptionLength, Convert.ToInt32(page),
                                                            pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BinhLuan>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0; return new List<BinhLuan>();
            }
        }
        #endregion
    }
}
